package com.ruoyi.deal.deal_service.service.impl;

import java.util.List;

import com.ruoyi.comment.domain.SystComment;
import com.ruoyi.comment.service.ISystCommentService;
import com.ruoyi.commerce.domain.CommerceInfo;
import com.ruoyi.commerce.service.ICommerceInfoService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.deal.deal_service.domain.DealInfoService;
import com.ruoyi.deal.deal_service.domain.InfoToDealService;
import com.ruoyi.deal.deal_service.mapper.DealServiceMapper;
import com.ruoyi.deal.deal_service.service.IDealInfoServiceService;
import com.ruoyi.deal.deal_service.service.IDealServiceService;
import com.ruoyi.deal.info.domain.DealInfo;
import com.ruoyi.deal.info.service.IDealInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 交易服务信息Service业务层处理
 * 
 * @author zhezhi
 * @date 2022-10-06
 */
@Service
public class DealServiceServiceImpl implements IDealServiceService
{
    @Autowired
    private DealServiceMapper dealServiceMapper;

    @Autowired
    private IDealInfoServiceService infoServiceService;

    @Autowired
    private IDealInfoService iDealInfoService;

    @Autowired
    private ICommerceInfoService commerceInfoService;

    /**
     * 查询交易服务信息
     * 
     * @param serviceId 交易服务信息主键
     * @return 交易服务信息
     */
    @Override
    public InfoToDealService selectDealServiceByServiceId(Long serviceId)
    {
        InfoToDealService infoToDealService = dealServiceMapper.selectDealServiceByServiceId(serviceId);
        DealInfoService dealInfoService = infoServiceService.selectDealInfoServiceByDealId(serviceId);
        DealInfo dealInfo = iDealInfoService.selectDealInfoByInfoId(dealInfoService.getDealId());
        Long merchantId = dealInfo.getMerchantId();
        CommerceInfo commerceInfo = commerceInfoService.selectCommerceInfoByInfoId(merchantId);
        infoToDealService.setCommerceName(commerceInfo.getInfoName());
        return infoToDealService;
    }
    

    /**
     * 查询交易服务信息列表
     * 
     * @param InfoToDealService 交易服务信息
     * @return 交易服务信息
     */
    @Override
    public List<InfoToDealService> selectDealServiceList(InfoToDealService InfoToDealService)
    {
        List<InfoToDealService> infoToDealServices = dealServiceMapper.selectDealServiceList(InfoToDealService);
        for(InfoToDealService info:infoToDealServices){
            if(info.getServiceId()==null) break;
            Long serviceId = info.getServiceId();
            DealInfoService dealInfoService = infoServiceService.selectDealInfoServiceByServiceId(serviceId);
            DealInfo dealInfo = iDealInfoService.selectDealInfoByInfoId(dealInfoService.getDealId());
            Long merchantId = dealInfo.getMerchantId();
            CommerceInfo commerceInfo = commerceInfoService.selectCommerceInfoByInfoId(merchantId);
            info.setCommerceName(commerceInfo.getInfoName());
        }
//        for(InfoToDealService infos:infoToDealServices){
//            System.out.println(infos);
//            infos.toString();
//        }
        return infoToDealServices;
    }

    /**
     * 新增交易服务信息
     * 
     * @param InfoToDealService 交易服务信息
     * @return 结果
     */
    @Override
    public int insertDealService(InfoToDealService InfoToDealService)
    {
        InfoToDealService.setCreateTime(DateUtils.getNowDate());
        int i = dealServiceMapper.insertDealService(InfoToDealService);
        InfoToDealService infoToDealService = dealServiceMapper.selectDealServiceByEnd();
        Long serviceId = infoToDealService.getServiceId();
        DealInfoService dealInfoService = new DealInfoService();
        dealInfoService.setServiceId(serviceId);
        dealInfoService.setDealId(InfoToDealService.getDealId());
        int i1 = infoServiceService.insertDealInfoService(dealInfoService);
        return i;
    }

    /**
     * 修改交易服务信息
     * 
     * @param InfoToDealService 交易服务信息
     * @return 结果
     */
    @Override
    public int updateDealService(InfoToDealService InfoToDealService)
    {
        InfoToDealService.setUpdateTime(DateUtils.getNowDate());
        InfoToDealService infoToDealService = dealServiceMapper.selectDealServiceByServiceId(InfoToDealService.getServiceId());

        if(infoToDealService.getDealId() != InfoToDealService.getDealId()){
            DealInfoService dealInfoService = new DealInfoService();
            dealInfoService.setDealId(InfoToDealService.getDealId());
            dealInfoService.setDealId(InfoToDealService.getDealId());
            int i = infoServiceService.updateDealInfoServiceByServiceId(dealInfoService);
        }
        return dealServiceMapper.updateDealService(InfoToDealService);
    }

    /**
     * 批量删除交易服务信息
     * 
     * @param serviceIds 需要删除的交易服务信息主键
     * @return 结果
     */
    @Override
    public int deleteDealServiceByServiceIds(Long[] serviceIds)
    {
        return dealServiceMapper.deleteDealServiceByServiceIds(serviceIds);
    }

    /**
     * 删除交易服务信息信息
     * 
     * @param serviceId 交易服务信息主键
     * @return 结果
     */
    @Override
    public int deleteDealServiceByServiceId(Long serviceId)
    {
        return dealServiceMapper.deleteDealServiceByServiceId(serviceId);
    }
}
